package com.yiwyn.Creational.factory;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

public class SimpleFactoryDemo {

    public static void main(String[] args) {
        Shape square = ShapeFactory.getShape("square");
        System.out.println(square.draw());
    }

    /**
     * 图形工厂类
     */
    static class ShapeFactory {
        public static Shape getShape(String shapeType) {
            if (shapeType.equalsIgnoreCase("square")) {
                return new Square("square");
            } else if (shapeType.equalsIgnoreCase("triangle")) {
                return new Triangle("triangle");
            }
            return null;
        }
    }


    /**
     * 基本图形
     */
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    static class Shape {
        private String name;
        public String draw() {
            return name;
        }
    }

    /**
     * 正方形
     */
    @Data
    @EqualsAndHashCode(callSuper = true)
    static class Square extends Shape {
        public Square(String name) {
            super(name);
        }
    }

    /**
     * 三角形
     */
    @Data
    @EqualsAndHashCode(callSuper = true)
    static class Triangle extends Shape {
        public Triangle(String name) {
            super(name);
        }
    }


}
